﻿using System;
using System.Collections.Generic;
using System.Text;
using System;
using System.Data;
using System.Data.SqlClient;
using Company.BLL.WS.GC;
using Company.BLL.Utils;
namespace Company.BLL.GC
{
    public partial class PhuKienDangKy : EntityBase
    {
        #region Private members.

        protected string _SoHopDong = String.Empty;
        protected string _MahaiQuan = String.Empty;
        protected string _MaDoanhNghiep = String.Empty;
        protected DateTime _NgayKy = new DateTime(1900, 01, 01);
        protected string _SoPhuKien = String.Empty;
        protected string _MaPhuKien = String.Empty;
        protected DateTime _NgayPhuKien = new DateTime(1900, 01, 01);
        protected string _NoiDung = String.Empty;
        protected string _VBCP = String.Empty;
        protected string _GhiChu = String.Empty;
        protected string _ThongTinCu = String.Empty;
        protected string _ThongTinMoi = String.Empty;
        protected string _MaDaiLy = String.Empty;
        protected long _SoTiepNhan;
        protected DateTime _NgayTiepNhan = new DateTime(1900, 01, 01);

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public string SoHopDong
        {
            set { this._SoHopDong = value; }
            get { return this._SoHopDong; }
        }
        public string MahaiQuan
        {
            set { this._MahaiQuan = value; }
            get { return this._MahaiQuan; }
        }
        public string MaDoanhNghiep
        {
            set { this._MaDoanhNghiep = value; }
            get { return this._MaDoanhNghiep; }
        }
        public DateTime NgayKy
        {
            set { this._NgayKy = value; }
            get { return this._NgayKy; }
        }
        public string SoPhuKien
        {
            set { this._SoPhuKien = value; }
            get { return this._SoPhuKien; }
        }
        public string MaPhuKien
        {
            set { this._MaPhuKien = value; }
            get { return this._MaPhuKien; }
        }
        public DateTime NgayPhuKien
        {
            set { this._NgayPhuKien = value; }
            get { return this._NgayPhuKien; }
        }
        public string NoiDung
        {
            set { this._NoiDung = value; }
            get { return this._NoiDung; }
        }
        public string VBCP
        {
            set { this._VBCP = value; }
            get { return this._VBCP; }
        }
        public string GhiChu
        {
            set { this._GhiChu = value; }
            get { return this._GhiChu; }
        }
        public string ThongTinCu
        {
            set { this._ThongTinCu = value; }
            get { return this._ThongTinCu; }
        }
        public string ThongTinMoi
        {
            set { this._ThongTinMoi = value; }
            get { return this._ThongTinMoi; }
        }
        public string MaDaiLy
        {
            set { this._MaDaiLy = value; }
            get { return this._MaDaiLy; }
        }
        public long SoTiepNhan
        {
            set { this._SoTiepNhan = value; }
            get { return this._SoTiepNhan; }
        }
        public DateTime NgayTiepNhan
        {
            set { this._NgayTiepNhan = value; }
            get { return this._NgayTiepNhan; }
        }

        //---------------------------------------------------------------------------------------------

        public bool IsExist
        {
            get
            {
                return this.Load();
            }
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public bool Load()
        {
            string spName = "p_GC_PhuKienDangKy_Load";
            SqlCommand dbCommand = (SqlCommand)this.db.GetStoredProcCommand(spName);

            this.db.AddInParameter(dbCommand, "@SoHopDong", SqlDbType.VarChar, this._SoHopDong);
            this.db.AddInParameter(dbCommand, "@MahaiQuan", SqlDbType.Char, this._MahaiQuan);
            this.db.AddInParameter(dbCommand, "@MaDoanhNghiep", SqlDbType.VarChar, this._MaDoanhNghiep);
            this.db.AddInParameter(dbCommand, "@NgayKy", SqlDbType.DateTime, this._NgayKy);
            this.db.AddInParameter(dbCommand, "@SoPhuKien", SqlDbType.VarChar, this._SoPhuKien);
            this.db.AddInParameter(dbCommand, "@MaPhuKien", SqlDbType.Char, this._MaPhuKien);
            this.db.AddInParameter(dbCommand, "@NgayPhuKien", SqlDbType.DateTime, this._NgayPhuKien);

            IDataReader reader = this.db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                if (!reader.IsDBNull(reader.GetOrdinal("SoHopDong"))) this._SoHopDong = reader.GetString(reader.GetOrdinal("SoHopDong"));
                if (!reader.IsDBNull(reader.GetOrdinal("MahaiQuan"))) this._MahaiQuan = reader.GetString(reader.GetOrdinal("MahaiQuan"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDoanhNghiep"))) this._MaDoanhNghiep = reader.GetString(reader.GetOrdinal("MaDoanhNghiep"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayKy"))) this._NgayKy = reader.GetDateTime(reader.GetOrdinal("NgayKy"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoPhuKien"))) this._SoPhuKien = reader.GetString(reader.GetOrdinal("SoPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaPhuKien"))) this._MaPhuKien = reader.GetString(reader.GetOrdinal("MaPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayPhuKien"))) this._NgayPhuKien = reader.GetDateTime(reader.GetOrdinal("NgayPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NoiDung"))) this._NoiDung = reader.GetString(reader.GetOrdinal("NoiDung"));
                if (!reader.IsDBNull(reader.GetOrdinal("VBCP"))) this._VBCP = reader.GetString(reader.GetOrdinal("VBCP"));
                if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) this._GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) this._ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinMoi"))) this._ThongTinMoi = reader.GetString(reader.GetOrdinal("ThongTinMoi"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDaiLy"))) this._MaDaiLy = reader.GetString(reader.GetOrdinal("MaDaiLy"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) this._SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) this._NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
                return true;
            }
            return false;
        }

        //---------------------------------------------------------------------------------------------

        public PhuKienDangKyCollection SelectCollectionBy_SoHopDong_AND_MahaiQuan_AND_MaDoanhNghiep_GC_AND_NgayKy()
        {
            string spName = "p_GC_PhuKienDangKy_SelectBy_SoHopDong_AND_MahaiQuan_AND_MaDoanhNghiep_GC_AND_NgayKy";
            SqlCommand dbCommand = (SqlCommand)this.db.GetStoredProcCommand(spName);

            this.db.AddInParameter(dbCommand, "@SoHopDong", SqlDbType.VarChar, this._SoHopDong);
            this.db.AddInParameter(dbCommand, "@MahaiQuan", SqlDbType.Char, this._MahaiQuan);
            this.db.AddInParameter(dbCommand, "@MaDoanhNghiep", SqlDbType.VarChar, this._MaDoanhNghiep);
            this.db.AddInParameter(dbCommand, "@NgayKy", SqlDbType.DateTime, this._NgayKy);

            PhuKienDangKyCollection collection = new PhuKienDangKyCollection();
            IDataReader reader = this.db.ExecuteReader(dbCommand);
            while (reader.Read())
            {
                PhuKienDangKy entity = new PhuKienDangKy();
                if (!reader.IsDBNull(reader.GetOrdinal("SoHopDong"))) entity.SoHopDong = reader.GetString(reader.GetOrdinal("SoHopDong"));
                if (!reader.IsDBNull(reader.GetOrdinal("MahaiQuan"))) entity.MahaiQuan = reader.GetString(reader.GetOrdinal("MahaiQuan"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDoanhNghiep"))) entity.MaDoanhNghiep = reader.GetString(reader.GetOrdinal("MaDoanhNghiep"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayKy"))) entity.NgayKy = reader.GetDateTime(reader.GetOrdinal("NgayKy"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoPhuKien"))) entity.SoPhuKien = reader.GetString(reader.GetOrdinal("SoPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaPhuKien"))) entity.MaPhuKien = reader.GetString(reader.GetOrdinal("MaPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayPhuKien"))) entity.NgayPhuKien = reader.GetDateTime(reader.GetOrdinal("NgayPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NoiDung"))) entity.NoiDung = reader.GetString(reader.GetOrdinal("NoiDung"));
                if (!reader.IsDBNull(reader.GetOrdinal("VBCP"))) entity.VBCP = reader.GetString(reader.GetOrdinal("VBCP"));
                if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) entity.ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinMoi"))) entity.ThongTinMoi = reader.GetString(reader.GetOrdinal("ThongTinMoi"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDaiLy"))) entity.MaDaiLy = reader.GetString(reader.GetOrdinal("MaDaiLy"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) entity.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
                collection.Add(entity);
            }
            return collection;
        }

        //---------------------------------------------------------------------------------------------
        public PhuKienDangKyCollection SelectCollectionDynamicRegister(string whereCondition, string orderByExpression)
        {
            PhuKienDangKyCollection collection = new PhuKienDangKyCollection();

            IDataReader reader = this.SelectReaderDynamicRegister(whereCondition, orderByExpression);
            while (reader.Read())
            {
                PhuKienDangKy entity = new PhuKienDangKy();

                if (!reader.IsDBNull(reader.GetOrdinal("SoHopDong"))) entity.SoHopDong = reader.GetString(reader.GetOrdinal("SoHopDong"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoPhuKien"))) entity.SoPhuKien = reader.GetString(reader.GetOrdinal("SoPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaPhuKien"))) entity.MaPhuKien = reader.GetString(reader.GetOrdinal("MaPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayPhuKien"))) entity.NgayPhuKien = reader.GetDateTime(reader.GetOrdinal("NgayPhuKien"));
                collection.Add(entity);
            }
            reader.Close();
            return collection;
        }
        public IDataReader SelectReaderDynamicRegister(string whereCondition, string orderByExpression)
        {
            string spName = "p_GC_PhuKienDangKy_SelectDynamicRegister";
            SqlCommand dbCommand = (SqlCommand)this.db.GetStoredProcCommand(spName);

            this.db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            this.db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return this.db.ExecuteReader(dbCommand);
        }
        public DataSet SelectBy_SoHopDong_AND_MahaiQuan_AND_MaDoanhNghiep_GC_AND_NgayKy()
        {
            string spName = "p_GC_PhuKienDangKy_SelectBy_SoHopDong_AND_MahaiQuan_AND_MaDoanhNghiep_GC_AND_NgayKy";
            SqlCommand dbCommand = (SqlCommand)this.db.GetStoredProcCommand(spName);

            this.db.AddInParameter(dbCommand, "@SoHopDong", SqlDbType.VarChar, this._SoHopDong);
            this.db.AddInParameter(dbCommand, "@MahaiQuan", SqlDbType.Char, this._MahaiQuan);
            this.db.AddInParameter(dbCommand, "@MaDoanhNghiep", SqlDbType.VarChar, this._MaDoanhNghiep);
            this.db.AddInParameter(dbCommand, "@NgayKy", SqlDbType.DateTime, this._NgayKy);

            return this.db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------

        public DataSet SelectAll()
        {
            string spName = "p_GC_PhuKienDangKy_SelectAll";
            SqlCommand dbCommand = (SqlCommand)this.db.GetStoredProcCommand(spName);
            return this.db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public IDataReader SelectReaderAll()
        {
            string spName = "p_GC_PhuKienDangKy_SelectAll";
            SqlCommand dbCommand = (SqlCommand)this.db.GetStoredProcCommand(spName);
            return this.db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "p_GC_PhuKienDangKy_SelectDynamic";
            SqlCommand dbCommand = (SqlCommand)this.db.GetStoredProcCommand(spName);

            this.db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            this.db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return this.db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "p_GC_PhuKienDangKy_SelectDynamic";
            SqlCommand dbCommand = (SqlCommand)this.db.GetStoredProcCommand(spName);

            this.db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            this.db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return this.db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public PhuKienDangKyCollection SelectCollectionAll()
        {
            PhuKienDangKyCollection collection = new PhuKienDangKyCollection();

            IDataReader reader = this.SelectReaderAll();
            while (reader.Read())
            {
                PhuKienDangKy entity = new PhuKienDangKy();

                if (!reader.IsDBNull(reader.GetOrdinal("SoHopDong"))) entity.SoHopDong = reader.GetString(reader.GetOrdinal("SoHopDong"));
                if (!reader.IsDBNull(reader.GetOrdinal("MahaiQuan"))) entity.MahaiQuan = reader.GetString(reader.GetOrdinal("MahaiQuan"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDoanhNghiep"))) entity.MaDoanhNghiep = reader.GetString(reader.GetOrdinal("MaDoanhNghiep"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayKy"))) entity.NgayKy = reader.GetDateTime(reader.GetOrdinal("NgayKy"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoPhuKien"))) entity.SoPhuKien = reader.GetString(reader.GetOrdinal("SoPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaPhuKien"))) entity.MaPhuKien = reader.GetString(reader.GetOrdinal("MaPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayPhuKien"))) entity.NgayPhuKien = reader.GetDateTime(reader.GetOrdinal("NgayPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NoiDung"))) entity.NoiDung = reader.GetString(reader.GetOrdinal("NoiDung"));
                if (!reader.IsDBNull(reader.GetOrdinal("VBCP"))) entity.VBCP = reader.GetString(reader.GetOrdinal("VBCP"));
                if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) entity.ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinMoi"))) entity.ThongTinMoi = reader.GetString(reader.GetOrdinal("ThongTinMoi"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDaiLy"))) entity.MaDaiLy = reader.GetString(reader.GetOrdinal("MaDaiLy"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) entity.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
                collection.Add(entity);
            }
            return collection;
        }

        //---------------------------------------------------------------------------------------------

        public PhuKienDangKyCollection SelectCollectionDynamic(string whereCondition, string orderByExpression)
        {
            PhuKienDangKyCollection collection = new PhuKienDangKyCollection();

            IDataReader reader = this.SelectReaderDynamic(whereCondition, orderByExpression);
            while (reader.Read())
            {
                PhuKienDangKy entity = new PhuKienDangKy();

                if (!reader.IsDBNull(reader.GetOrdinal("SoHopDong"))) entity.SoHopDong = reader.GetString(reader.GetOrdinal("SoHopDong"));
                if (!reader.IsDBNull(reader.GetOrdinal("MahaiQuan"))) entity.MahaiQuan = reader.GetString(reader.GetOrdinal("MahaiQuan"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDoanhNghiep"))) entity.MaDoanhNghiep = reader.GetString(reader.GetOrdinal("MaDoanhNghiep"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayKy"))) entity.NgayKy = reader.GetDateTime(reader.GetOrdinal("NgayKy"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoPhuKien"))) entity.SoPhuKien = reader.GetString(reader.GetOrdinal("SoPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaPhuKien"))) entity.MaPhuKien = reader.GetString(reader.GetOrdinal("MaPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayPhuKien"))) entity.NgayPhuKien = reader.GetDateTime(reader.GetOrdinal("NgayPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NoiDung"))) entity.NoiDung = reader.GetString(reader.GetOrdinal("NoiDung"));
                if (!reader.IsDBNull(reader.GetOrdinal("VBCP"))) entity.VBCP = reader.GetString(reader.GetOrdinal("VBCP"));
                if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) entity.ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinMoi"))) entity.ThongTinMoi = reader.GetString(reader.GetOrdinal("ThongTinMoi"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDaiLy"))) entity.MaDaiLy = reader.GetString(reader.GetOrdinal("MaDaiLy"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) entity.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
                collection.Add(entity);
            }
            return collection;
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public long Insert()
        {
            return this.InsertTransaction(null);
        }

        //---------------------------------------------------------------------------------------------

        public long InsertTransaction(SqlTransaction transaction)
        {
            string spName = "p_GC_PhuKienDangKy_Insert";
            SqlCommand dbCommand = (SqlCommand)this.db.GetStoredProcCommand(spName);
            this.db.AddInParameter(dbCommand, "@SoHopDong", SqlDbType.VarChar, this._SoHopDong);
            this.db.AddInParameter(dbCommand, "@MahaiQuan", SqlDbType.Char, this._MahaiQuan);
            this.db.AddInParameter(dbCommand, "@MaDoanhNghiep", SqlDbType.VarChar, this._MaDoanhNghiep);
            this.db.AddInParameter(dbCommand, "@NgayKy", SqlDbType.DateTime, this._NgayKy);
            this.db.AddInParameter(dbCommand, "@SoPhuKien", SqlDbType.VarChar, this._SoPhuKien);
            this.db.AddInParameter(dbCommand, "@MaPhuKien", SqlDbType.Char, this._MaPhuKien);
            this.db.AddInParameter(dbCommand, "@NgayPhuKien", SqlDbType.DateTime, this._NgayPhuKien);
            this.db.AddInParameter(dbCommand, "@NoiDung", SqlDbType.NVarChar, this._NoiDung);
            this.db.AddInParameter(dbCommand, "@VBCP", SqlDbType.NVarChar, this._VBCP);
            this.db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            this.db.AddInParameter(dbCommand, "@ThongTinCu", SqlDbType.NVarChar, this._ThongTinCu);
            this.db.AddInParameter(dbCommand, "@ThongTinMoi", SqlDbType.NVarChar, this._ThongTinMoi);
            this.db.AddInParameter(dbCommand, "@MaDaiLy", SqlDbType.VarChar, this._MaDaiLy);
            this.db.AddInParameter(dbCommand, "@SoTiepNhan", SqlDbType.BigInt, this._SoTiepNhan);
            this.db.AddInParameter(dbCommand, "@NgayTiepNhan", SqlDbType.DateTime, this._NgayTiepNhan);

            if (transaction != null)
            {
                return this.db.ExecuteNonQuery(dbCommand, transaction);
            }
            else
            {
                return this.db.ExecuteNonQuery(dbCommand);
            }
        }

        //---------------------------------------------------------------------------------------------

        public bool Insert(PhuKienDangKyCollection collection)
        {
            bool ret;
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    bool ret01 = true;
                    foreach (PhuKienDangKy item in collection)
                    {
                        if (item.InsertTransaction(transaction) <= 0)
                        {
                            ret01 = false;
                            break;
                        }
                    }
                    if (ret01)
                    {
                        transaction.Commit();
                        ret = true;
                    }
                    else
                    {
                        transaction.Rollback();
                        ret = false;
                    }
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally
                {
                    connection.Close();
                }
            }
            return ret;
        }

        //---------------------------------------------------------------------------------------------		

        public void InsertTransaction(SqlTransaction transaction, PhuKienDangKyCollection collection)
        {
            foreach (PhuKienDangKy item in collection)
            {
                item.InsertTransaction(transaction);
            }
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert / Update methods.
        public int InsertUpdate()
        {
            return this.InsertUpdateTransaction(null);
        }

        //---------------------------------------------------------------------------------------------

        public int InsertUpdateTransaction(SqlTransaction transaction)
        {
            string spName = "p_GC_PhuKienDangKy_InsertUpdate";
            SqlCommand dbCommand = (SqlCommand)this.db.GetStoredProcCommand(spName);
            this.db.AddInParameter(dbCommand, "@SoHopDong", SqlDbType.VarChar, this._SoHopDong);
            this.db.AddInParameter(dbCommand, "@MahaiQuan", SqlDbType.Char, this._MahaiQuan);
            this.db.AddInParameter(dbCommand, "@MaDoanhNghiep", SqlDbType.VarChar, this._MaDoanhNghiep);
            this.db.AddInParameter(dbCommand, "@NgayKy", SqlDbType.DateTime, this._NgayKy);
            this.db.AddInParameter(dbCommand, "@SoPhuKien", SqlDbType.VarChar, this._SoPhuKien);
            this.db.AddInParameter(dbCommand, "@MaPhuKien", SqlDbType.Char, this._MaPhuKien);
            this.db.AddInParameter(dbCommand, "@NgayPhuKien", SqlDbType.DateTime, this._NgayPhuKien);
            this.db.AddInParameter(dbCommand, "@NoiDung", SqlDbType.NVarChar, this._NoiDung);
            this.db.AddInParameter(dbCommand, "@VBCP", SqlDbType.NVarChar, this._VBCP);
            this.db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            this.db.AddInParameter(dbCommand, "@ThongTinCu", SqlDbType.NVarChar, this._ThongTinCu);
            this.db.AddInParameter(dbCommand, "@ThongTinMoi", SqlDbType.NVarChar, this._ThongTinMoi);
            this.db.AddInParameter(dbCommand, "@MaDaiLy", SqlDbType.VarChar, this._MaDaiLy);
            this.db.AddInParameter(dbCommand, "@SoTiepNhan", SqlDbType.BigInt, this._SoTiepNhan);
            this.db.AddInParameter(dbCommand, "@NgayTiepNhan", SqlDbType.DateTime, this._NgayTiepNhan);

            if (transaction != null)
                return this.db.ExecuteNonQuery(dbCommand, transaction);
            else
                return this.db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public bool InsertUpdate(PhuKienDangKyCollection collection)
        {
            bool ret;
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    bool ret01 = true;
                    foreach (PhuKienDangKy item in collection)
                    {
                        if (item.InsertUpdateTransaction(transaction) <= 0)
                        {
                            ret01 = false;
                            break;
                        }
                    }
                    if (ret01)
                    {
                        transaction.Commit();
                        ret = true;
                    }
                    else
                    {
                        transaction.Rollback();
                        ret = false;
                    }
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally
                {
                    connection.Close();
                }
            }
            return ret;
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public int Update()
        {
            return this.UpdateTransaction(null);
        }

        //---------------------------------------------------------------------------------------------

        public int UpdateTransaction(SqlTransaction transaction)
        {
            string spName = "p_GC_PhuKienDangKy_Update";
            SqlCommand dbCommand = (SqlCommand)this.db.GetStoredProcCommand(spName);

            this.db.AddInParameter(dbCommand, "@SoHopDong", SqlDbType.VarChar, this._SoHopDong);
            this.db.AddInParameter(dbCommand, "@MahaiQuan", SqlDbType.Char, this._MahaiQuan);
            this.db.AddInParameter(dbCommand, "@MaDoanhNghiep", SqlDbType.VarChar, this._MaDoanhNghiep);
            this.db.AddInParameter(dbCommand, "@NgayKy", SqlDbType.DateTime, this._NgayKy);
            this.db.AddInParameter(dbCommand, "@SoPhuKien", SqlDbType.VarChar, this._SoPhuKien);
            this.db.AddInParameter(dbCommand, "@MaPhuKien", SqlDbType.Char, this._MaPhuKien);
            this.db.AddInParameter(dbCommand, "@NgayPhuKien", SqlDbType.DateTime, this._NgayPhuKien);
            this.db.AddInParameter(dbCommand, "@NoiDung", SqlDbType.NVarChar, this._NoiDung);
            this.db.AddInParameter(dbCommand, "@VBCP", SqlDbType.NVarChar, this._VBCP);
            this.db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            this.db.AddInParameter(dbCommand, "@ThongTinCu", SqlDbType.NVarChar, this._ThongTinCu);
            this.db.AddInParameter(dbCommand, "@ThongTinMoi", SqlDbType.NVarChar, this._ThongTinMoi);
            this.db.AddInParameter(dbCommand, "@MaDaiLy", SqlDbType.VarChar, this._MaDaiLy);
            this.db.AddInParameter(dbCommand, "@SoTiepNhan", SqlDbType.BigInt, this._SoTiepNhan);
            this.db.AddInParameter(dbCommand, "@NgayTiepNhan", SqlDbType.DateTime, this._NgayTiepNhan);

            if (transaction != null)
                return this.db.ExecuteNonQuery(dbCommand, transaction);
            else
                return this.db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public void UpdateCollection(PhuKienDangKyCollection collection, SqlTransaction transaction)
        {
            foreach (PhuKienDangKy item in collection)
            {
                item.UpdateTransaction(transaction);
            }
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public int Delete()
        {
            return this.DeleteTransaction(null);
        }

        //---------------------------------------------------------------------------------------------

        public int DeleteTransaction(SqlTransaction transaction)
        {
            string spName = "p_GC_PhuKienDangKy_Delete";
            SqlCommand dbCommand = (SqlCommand)this.db.GetStoredProcCommand(spName);

            this.db.AddInParameter(dbCommand, "@SoHopDong", SqlDbType.VarChar, this._SoHopDong);
            this.db.AddInParameter(dbCommand, "@MahaiQuan", SqlDbType.Char, this._MahaiQuan);
            this.db.AddInParameter(dbCommand, "@MaDoanhNghiep", SqlDbType.VarChar, this._MaDoanhNghiep);
            this.db.AddInParameter(dbCommand, "@NgayKy", SqlDbType.DateTime, this._NgayKy);
            this.db.AddInParameter(dbCommand, "@SoPhuKien", SqlDbType.VarChar, this._SoPhuKien);
            this.db.AddInParameter(dbCommand, "@MaPhuKien", SqlDbType.Char, this._MaPhuKien);
            this.db.AddInParameter(dbCommand, "@NgayPhuKien", SqlDbType.DateTime, this._NgayPhuKien);

            if (transaction != null)
                return this.db.ExecuteNonQuery(dbCommand, transaction);
            else
                return this.db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public void DeleteCollection(PhuKienDangKyCollection collection, SqlTransaction transaction)
        {
            foreach (PhuKienDangKy item in collection)
            {
                item.DeleteTransaction(transaction);
            }
        }

        //---------------------------------------------------------------------------------------------

        public bool DeleteCollection(PhuKienDangKyCollection collection)
        {
            bool ret;
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    bool ret01 = true;
                    foreach (PhuKienDangKy item in collection)
                    {
                        if (item.DeleteTransaction(transaction) <= 0)
                        {
                            ret01 = false;
                            break;
                        }
                    }
                    if (ret01)
                    {
                        transaction.Commit();
                        ret = true;
                    }
                    else
                    {
                        transaction.Rollback();
                        ret = false;
                    }
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally
                {
                    connection.Close();
                }
            }
            return ret;
        }
        #endregion



        //---------------------------------------------------------------------------------------------

        #region WebService

        public DataSet WS_GetDanhSachDaDangKy(string maHaiQuan, string maDoanhNghiep)
        {
            GCService service = new GCService();
            return service.PhuKien_GetDanhSach(maHaiQuan, maDoanhNghiep);
        }

        public void UpdateRegistedToDatabase(string maHaiQuan, string maDoanhNghiep)
        {
            // Lấy danh sách từ WEB SERVICE.
            GCService service = new GCService();
            DataSet ds = this.WS_GetDanhSachDaDangKy(maHaiQuan, maDoanhNghiep);
            DataSet dsHang = service.HangPhuKien_GetDanhSach(maHaiQuan, maDoanhNghiep);
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    //cap nhat vao csdl thong tin phu kien
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        PhuKienDangKy pkdk = new PhuKienDangKy();
                        pkdk.SoHopDong = row["SoHopDong"].ToString();
                        pkdk.MahaiQuan = row["MaHaiQuan"].ToString();
                        pkdk.MaDoanhNghiep = row["MaDoanhNghiep"].ToString();
                        pkdk.NgayKy = Convert.ToDateTime(row["NgayKy"]);
                        pkdk.SoPhuKien = row["SoPhuKien"].ToString();
                        pkdk.MaPhuKien = row["LoaiPhuKien"].ToString();
                        pkdk.NgayPhuKien = Convert.ToDateTime(row["NgayPhuKien"]);
                        pkdk.SoTiepNhan = Convert.ToInt64(row["SoTiepNhan"]);
                        pkdk.NgayTiepNhan = Convert.ToDateTime(row["NgayTiepNhan"]);
                        pkdk.NoiDung =FontConverter.TCVN2Unicode(row["NoiDung"].ToString());
                        pkdk.VBCP = FontConverter.TCVN2Unicode(row["VBCP"].ToString());
                        pkdk.GhiChu = FontConverter.TCVN2Unicode(row["GhiChu"].ToString());
                        pkdk.ThongTinCu =FontConverter.TCVN2Unicode(row["ThongTinCu"].ToString());
                        pkdk.ThongTinMoi = FontConverter.TCVN2Unicode(row["ThongTinMoi"].ToString());
                        pkdk.InsertUpdateTransaction(transaction);
                    }

                    //// Cập nhật vào CSDL hang phu kien                 
                    foreach (DataRow row in dsHang.Tables[0].Rows)
                    {
                        PhuKien pk = new PhuKien();
                        pk.SoHopDong = row["SoHopDong"].ToString();
                        pk.MahaiQuan = row["MaHaiQuan"].ToString();
                        pk.MaDoanhNghiep = row["MaDoanhNghiep"].ToString();
                        pk.NgayKy = Convert.ToDateTime(row["NgayKy"]);
                        pk.SoPhuKien = row["SoPhuKien"].ToString();
                        pk.MaPhuKien = row["LoaiPhuKien"].ToString();
                        pk.MaHang = row["Ma"].ToString().Substring(1);
                        pk.TenHang = FontConverter.TCVN2Unicode(row["Ten"].ToString());
                        pk.MaHS = row["MaHS"].ToString();
                        pk.DVT_ID = row["DVT_ID"].ToString();
                        pk.NhomSP = row["MaSanPham"].ToString();
                        pk.SoLuong = Convert.ToDecimal(row["SoLuong"]);
                        pk.ThongTinCuHang = FontConverter.TCVN2Unicode(row["ThongTin"].ToString());
                        pk.NuocXX_ID = row["NuocXX_ID"].ToString();
                        pk.TinhTrang = FontConverter.TCVN2Unicode(row["TinhTrang"].ToString());
                        try
                        {
                            pk.DonGia = Convert.ToDecimal(row["DonGia"].ToString());
                        }
                        catch { }
                        try
                        {
                            pk.TriGia = Convert.ToDecimal(row["TriGia"].ToString());
                        }
                        catch { }
                                              
                        pk.NguyenTe_ID = row["NguyenTe_ID"].ToString();
                        pk.STTHang = Convert.ToInt32(row["STT"].ToString());
                        pk.InsertUpdateTransaction(transaction);
                    }
                    transaction.Commit();

                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
                finally
                {
                    connection.Close();
                }
            }
        }
        public static void DongBoHaiQuan(string maHaiQuan, string maDoanhNghiep)
        {
            PhuKienDangKy pkdk = new PhuKienDangKy();
            pkdk.UpdateRegistedToDatabase(maHaiQuan, maDoanhNghiep);
        }
        public static void DongBoHaiQuan(string maHaiQuan, string maDoanhNghiep, string sohopdong, DateTime ngayky)
        {
            PhuKienDangKy pkdk = new PhuKienDangKy();
            pkdk.UpdateRegistedToDatabase(maHaiQuan, maDoanhNghiep, sohopdong, ngayky);
        }
        public void UpdateRegistedToDatabase(string maHaiQuan, string maDoanhNghiep, string sohopdong, DateTime ngayky)
        {
            // Lấy danh sách từ WEB SERVICE.
            GCService service = new GCService();
            DataSet ds = service.PhuKien_GetDanhSachOfHopDong(maHaiQuan, maDoanhNghiep, sohopdong, ngayky);
            DataSet dsHang = service.HangPhuKien_GetDanhSachOfHopDong(maHaiQuan, maDoanhNghiep, sohopdong, ngayky);
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    //cap nhat vao csdl thong tin phu kien
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        PhuKienDangKy pkdk = new PhuKienDangKy();
                        pkdk.SoHopDong = row["SoHopDong"].ToString();
                        pkdk.MahaiQuan = row["MaHaiQuan"].ToString();
                        pkdk.MaDoanhNghiep = row["MaDoanhNghiep"].ToString();
                        pkdk.NgayKy = Convert.ToDateTime(row["NgayKy"]);
                        pkdk.SoPhuKien = row["SoPhuKien"].ToString();
                        pkdk.MaPhuKien = row["LoaiPhuKien"].ToString();
                        pkdk.NgayPhuKien = Convert.ToDateTime(row["NgayPhuKien"]);
                        pkdk.SoTiepNhan = Convert.ToInt64(row["SoTiepNhan"]);
                        pkdk.NgayTiepNhan = Convert.ToDateTime(row["NgayTiepNhan"]);
                        pkdk.NoiDung = FontConverter.TCVN2Unicode(row["NoiDung"].ToString());
                        pkdk.VBCP = FontConverter.TCVN2Unicode(row["VBCP"].ToString());
                        pkdk.GhiChu = FontConverter.TCVN2Unicode(row["GhiChu"].ToString());
                        pkdk.ThongTinCu = FontConverter.TCVN2Unicode(row["ThongTinCu"].ToString());
                        pkdk.ThongTinMoi = FontConverter.TCVN2Unicode(row["ThongTinMoi"].ToString());
                        pkdk.InsertUpdateTransaction(transaction);
                    }

                    //// Cập nhật vào CSDL hang phu kien                 
                    foreach (DataRow row in dsHang.Tables[0].Rows)
                    {
                        PhuKien pk = new PhuKien();
                        pk.SoHopDong = row["SoHopDong"].ToString();
                        pk.MahaiQuan = row["MaHaiQuan"].ToString();
                        pk.MaDoanhNghiep = row["MaDoanhNghiep"].ToString();
                        pk.NgayKy = Convert.ToDateTime(row["NgayKy"]);
                        pk.SoPhuKien = row["SoPhuKien"].ToString();
                        pk.MaPhuKien = row["LoaiPhuKien"].ToString();
                        pk.MaHang = row["Ma"].ToString();
                        pk.TenHang = FontConverter.TCVN2Unicode(row["Ten"].ToString());
                        pk.MaHS = row["MaHS"].ToString();
                        pk.DVT_ID = row["DVT_ID"].ToString();
                        pk.NhomSP = row["MaSanPham"].ToString();
                        pk.SoLuong = Convert.ToDecimal(row["SoLuong"]);
                        pk.ThongTinCuHang = FontConverter.TCVN2Unicode(row["ThongTin"].ToString());
                        pk.NuocXX_ID = row["NuocXX_ID"].ToString();
                        pk.TinhTrang = FontConverter.TCVN2Unicode(row["TinhTrang"].ToString());
                        try
                        {
                            pk.DonGia = Convert.ToDecimal(row["DonGia"].ToString());
                        }
                        catch { }
                        try
                        {
                            pk.TriGia = Convert.ToDecimal(row["TriGia"].ToString());
                        }
                        catch { }
                        pk.NguyenTe_ID = row["NguyenTe_ID"].ToString();
                        pk.STTHang = Convert.ToInt32(row["STT"].ToString());
                        pk.InsertUpdateTransaction(transaction);
                    }
                    transaction.Commit();

                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
                finally
                {
                    connection.Close();
                }
            }
        }
        #endregion
    }
}